package com.uphone.kingmall.activity.order;

import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.china.zhuoyue.R;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.uphone.kingmall.activity.GoodsDetailActivity;
import com.uphone.kingmall.adapter.RvOrderFollowAdapter;
import com.uphone.kingmall.adapter.RvRefundAdapter;
import com.uphone.kingmall.base.BaseActivity;
import com.uphone.kingmall.bean.OrderRefundDetailBean;
import com.uphone.kingmall.listener.onNormalRequestListener;
import com.uphone.kingmall.utils.ConstansUtils;
import com.uphone.kingmall.utils.GsonUtils;
import com.uphone.kingmall.utils.MyUrl;
import com.uphone.kingmall.utils.OkGoUtils;
import com.uphone.kingmall.utils.intent.IntentData;
import com.uphone.kingmall.utils.intent.IntentUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * <pre>
 *     author : 孙增艳
 *     e-mail : sunzengyan@outlook.com
 *     time   : 2019/2/28
 *     desc   : 退款退货详情页
 *     version: 1.0
 * </pre>
 */
public class OrderRefundDetailsActivity extends BaseActivity {
    @BindView(R.id.tv_state)
    TextView tvState;
    @BindView(R.id.tv_date)
    TextView tvDate;
    @BindView(R.id.tv_price)
    TextView tvPrice;
    @BindView(R.id.rv_goods)
    RecyclerView rvGoods;
    @BindView(R.id.rv_wuliu)
    RecyclerView rvWuliu;
    @BindView(R.id.tv_order_number)
    TextView tvOrderNumber;
    @BindView(R.id.tv_order_time)
    TextView tvOrderTime;
    @BindView(R.id.tv_tuikuan_number)
    TextView tvTuikuanNumber;
    @IntentData
    private int id = -1;

    private RvRefundAdapter refundAdapter;
    private RvOrderFollowAdapter followAdapter;
    private OrderRefundDetailBean bean;

    @Override
    public int getLayoutContentId() {
        return R.layout.kingmall_activity_order_refund_details;
    }

    @Override
    public void initView() {
        setStatusBar();

        rvGoods.setLayoutManager(new LinearLayoutManager(this));

        rvWuliu.setLayoutManager(new LinearLayoutManager(this));
    }

    @Override
    public void loadJPush() {
        super.loadJPush();
        initData();
    }

    @Override
    public void initData() {
        HttpParams params = new HttpParams();
        params.put("orderId", id);
        OkGoUtils.progressRequest(MyUrl.getRefundInfo, this, params, new onNormalRequestListener() {
            @Override
            public void onSuccess(String body, int code) {
                if (code == 0) {
                    bean = GsonUtils.getGson().fromJson(body, OrderRefundDetailBean.class);
                    loadData();
                }
            }

            @Override
            public void onError(Response<String> response) {

            }
        });
    }


    private void loadData() {
        if (bean != null) {
            final OrderRefundDetailBean.RefundBean refund = bean.getRefund();
            if (refundAdapter == null) {
                refundAdapter = new RvRefundAdapter(this, refund.getGoodslist());
                rvGoods.setAdapter(refundAdapter);

                refundAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        IntentUtils.getInstance()
                                .with(OrderRefundDetailsActivity.this, GoodsDetailActivity.class)
                                .putInt(ConstansUtils.ID, refund.getGoodslist().get(position).getGoodsId())
                                .start();
                    }
                });
            } else {
                refundAdapter.setNewData(refund.getGoodslist());
            }
            if (bean.getTrace() != null) {
                followAdapter = new RvOrderFollowAdapter(OrderRefundDetailsActivity.this, bean.getTrace().getState());
                rvWuliu.setAdapter(followAdapter);
            }
            if (!TextUtils.isEmpty(refund.getReplyTime())) {
                tvDate.setText("" + refund.getApplyTime());
            } else {
                tvDate.setText("" + refund.getReplyTime());
            }
            tvPrice.setText("" + refund.getRefundFee());
            tvOrderNumber.setText("订单编号：" + refund.getOrderNo());
            tvOrderTime.setText("申请时间：" + refund.getApplyTime());
            tvTuikuanNumber.setText("退款编号：" + refund.getRefundNo());
            String stateStr = "";
            switch (refund.getRefundState()) {
                //	0:被驳回;1:成功;2申请中;3退款中
                case 0:
                    stateStr = "被驳回";
                    break;
                case 1:
                    stateStr = "退款成功";
                    break;
                case 2:
                    stateStr = "申请中";
                    break;
            }
            tvState.setText(stateStr);
        }

    }

    @OnClick(R.id.iv_back)
    public void onViewClicked() {
        finish();
    }
}
